package com.ruijie.acl.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.jboss.seam.annotations.Transactional;
import org.springframework.util.Assert;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.Ace;
import com.ruijie.acl.Template;
import com.ruijie.acl.TimeRange;
import com.ruijie.acl.task.model.AccessGroupInfo;
import com.ruijie.acl.type.AceTypeEnum;
import com.ruijie.acl.type.MacMatchTypeEnum;
import com.ruijie.acl.type.MatchTypeEnum;
import com.ruijie.acl.type.MoveDirection;
import com.ruijie.acl.type.ProtocolTypeEnum;
import com.ruijie.acl.utils.AclConfigUtil;
import com.ruijie.foundation.bo.EntityObject;
import com.ruijie.foundation.dao.support.Page;
import com.ruijie.acl.type.PortTypeEnum;
import com.ruijie.acl.type.EthernetTypeEnum;;

public class AceServiceImpl extends AclHibernateEntityExtendDao<Ace> implements
		AceService {
	/**
	 * 根据模板分页查询ACL规则
	 * 
	 * @param entity
	 *            模板
	 * @param pageNo
	 *            页数
	 * @param pageSize
	 *            每面显示的记录数
	 * @return ACL规则
	 */
	public Page pageQueryByTemplate(Template entity, int pageNo, int pageSize) {
		Assert.notNull(entity);
		Criteria criteria = createCriteria(Ace.class);
		criteria.add(Restrictions.eq("template", entity));
		criteria.addOrder(org.hibernate.criterion.Order.asc("aceNum"));
		return pagedQuery(criteria, pageNo, pageSize);
	}

	/**
	 * 根据时间段分页查询ACL规则
	 * 
	 * @param entity
	 *            时间段

	 * @param pageNo
	 *            页数
	 * @param pageSize
	 *            每面显示的记录数
	 * @return ACL规则
	 */
	public Page pageQueryByTimeRange(TimeRange entity, int pageNo, int pageSize) { 
		Assert.notNull(entity);
		String hql = "select ace from Ace ace where (ace.template is not null and ace.accessGroup is null or ace.accessGroup.id in(select ag.id from Ace ace1,AccessGroup ag where ag.aclDevice is null and ace1.accessGroup=ag and ace1.timeRange=?)) and ace.timeRange=? order by ace.template.id,ace.accessGroup.id";
		return pagedQuery(hql, pageNo, pageSize, entity,entity);
	}

	/**
	 * 根据ACL组分页查询ACL规则
	 * 
	 * @param entity
	 *            ACL组

	 * @param pageNo
	 *            页数
	 * @param pageSize
	 *            每面显示的记录数
	 * @return ACL规则
	 */
	public Page pageQueryByAccessGroup(AccessGroup entity, int pageNo,
			int pageSize) {
		Assert.notNull(entity);
		Criteria criteria = createCriteria(Ace.class);
		criteria.add(Restrictions.eq("accessGroup", entity));
		criteria.addOrder(org.hibernate.criterion.Order.asc("aceNum"));
		return pagedQuery(criteria, pageNo, pageSize);
	}

	/**
	 * 根据ACL组取得ACL规则
	 * 
	 * @param entity
	 *            ACL组

	 * @return ACL规则
	 */
	@SuppressWarnings("unchecked")
	public List<Ace> getByAccessGroup(AccessGroup entity) {
		Assert.notNull(entity);
		Criteria criteria = createCriteria(Ace.class);
		criteria.add(Restrictions.eq("accessGroup", entity));
		criteria.addOrder(org.hibernate.criterion.Order.asc("aceNum"));
		return (List<Ace>) (criteria.list());
	}

	/**
	 * 保存规则
	 * 
	 * @param entity
	 *            规则
	 */
	@Override
	public void save(Ace entity) {
		int aceNum = getNextAceNum(entity);
		if (entity.getAceNum() == null) {
			entity.setAceNum(0);
		}
		if (entity.getAceNum().intValue() <= 0) {
			// 未填写规则号，自动加到最后

			entity.setAceNum(aceNum);
		} else {
			if (entity.getAceNum().intValue() >= aceNum) {
				entity.setAceNum(aceNum);
			} else {
				// 顺序中已存在,将比此顺序大的值加1并进行更新

				String hql = "";
				if (entity.getAccessGroup() != null) {
					hql = "UPDATE Ace SET aceNum=aceNum+1 WHERE aceNum>=? AND accessGroup=?";
					this.createQuery(hql, entity.getAceNum(),
							entity.getAccessGroup()).executeUpdate();
				} else if (entity.getTemplate() != null) {
					hql = "UPDATE Ace SET aceNum=aceNum+1 WHERE aceNum>=? AND template=?";
					this.createQuery(hql, entity.getAceNum(),
							entity.getTemplate()).executeUpdate();
				}
			}
		}
		if(entity.getTimeRange()!=null){
			entity.setTimeRangeName(entity.getTimeRange().getName());
		}
		super.save(entity);
	}

	/**
	 * 更新规则
	 * 
	 * @param entity
	 *            规则
	 */
	@Override
	public void update(Ace entity) {
		if(entity.getTimeRange()!=null){
			entity.setTimeRangeName(entity.getTimeRange().getName());
		}
		super.save(entity);
	}
	/**
	 * 更新ACE序号
	 * @param entity ACL规则
	 * @param aceNumNew 修改后的ACE序号
	 */
	public void update(Ace entity, Integer aceNumNew) {
		//若AceNum修改,则进行序号调整

		if (aceNumNew != null && aceNumNew > 0 && entity.getAceNum().intValue() != aceNumNew.intValue()){
			String hql = "";
			int tempAceNumNew = aceNumNew.intValue();
			int tempAceNumOld = entity.getAceNum().intValue();
			int nextAceNum = getNextAceNum(entity);
			if (tempAceNumNew >= nextAceNum){
				tempAceNumNew = nextAceNum - 1;
			}
			if (tempAceNumNew < tempAceNumOld){
				//往上移
				if (entity.getAccessGroup() != null) {
					hql = "UPDATE Ace SET aceNum=aceNum+1 WHERE aceNum>=? AND aceNum<? AND accessGroup=?";
					this.createQuery(hql, tempAceNumNew, tempAceNumOld,
							entity.getAccessGroup()).executeUpdate();
				} else if (entity.getTemplate() != null) {
					hql = "UPDATE Ace SET aceNum=aceNum+1 WHERE aceNum>=? AND aceNum<? AND template=?";
					this.createQuery(hql, tempAceNumNew, tempAceNumOld,
							entity.getTemplate()).executeUpdate();
				}
			}else{
				//往下移
				if (entity.getAccessGroup() != null) {
					hql = "UPDATE Ace SET aceNum=aceNum-1 WHERE aceNum>? AND aceNum<=? AND accessGroup=?";
					this.createQuery(hql, tempAceNumOld, tempAceNumNew,
							entity.getAccessGroup()).executeUpdate();
				} else if (entity.getTemplate() != null) {
					hql = "UPDATE Ace SET aceNum=aceNum-1 WHERE aceNum>? AND aceNum<=? AND template=?";
					this.createQuery(hql, tempAceNumOld, tempAceNumNew,
							entity.getTemplate()).executeUpdate();
				}
			}
			entity.setAceNum(tempAceNumNew);
		}
		if(entity.getTimeRange()!=null){
			entity.setTimeRangeName(entity.getTimeRange().getName());
		}
		super.save(entity);
	}
	/**
	 * 删除规则
	 * 
	 * @param entity
	 *            规则
	 */
	@Override
	public void remove(Ace entity) {
		Integer aceNum = entity.getAceNum();
		AccessGroup accessGroup = entity.getAccessGroup();
		Template template = entity.getTemplate();
		super.remove(entity);
		String hql = "";
		if (accessGroup != null) {
			hql = "UPDATE Ace SET aceNum=aceNum-1 WHERE aceNum>? AND accessGroup=?";
			this.createQuery(hql, aceNum, accessGroup).executeUpdate();
		} else if (template != null) {
			hql = "UPDATE Ace SET aceNum=aceNum-1 WHERE aceNum>? AND template=?";
			this.createQuery(hql, aceNum, template).executeUpdate();
		}
	}

	/**
	 * 取得下一个规则号
	 * 
	 * @param entity
	 *            规则
	 * @return 规则号

	 */
	public Integer getNextAceNum(Ace entity) {
		Assert.notNull(entity);
		Integer nextAceNum = 0;
		String hql = "";
		if (entity.getAccessGroup() != null) {
			hql = "SELECT MAX(aceNum) FROM Ace WHERE accessGroup=?";
			nextAceNum = (Integer) (this.createQuery(hql, entity
					.getAccessGroup()).uniqueResult());
		} else if (entity.getTemplate() != null) {
			hql = "SELECT MAX(aceNum) FROM Ace WHERE template=?";
			nextAceNum = (Integer) (this.createQuery(hql, entity.getTemplate())
					.uniqueResult());
		}
		if (nextAceNum != null) {
			nextAceNum = nextAceNum + 1;
		} else {
			nextAceNum = 1;
		}
		return nextAceNum;
	}

	/**
	 * 根据规则号判断规则是否存在

	 * 
	 * @param accessGroup
	 *            ACL组

	 * @param aceNum
	 *            规则号

	 * @return 规则是否存在
	 */
	public boolean existByAceNum(AccessGroup accessGroup, Integer aceNum) {
		Assert.notNull(accessGroup);
		Criteria criteria = createCriteria(Ace.class);
		criteria.add(Restrictions.eq("accessGroup", accessGroup));
		criteria.add(Restrictions.eq("aceNum", aceNum));
		int count = (Integer) criteria.setProjection(Projections.rowCount())
				.uniqueResult();
		return count > 0;
	}

	/**
	 * 根据移动方向更新规则号

	 * 
	 * @param entity
	 * @param direction
	 */
	@SuppressWarnings("unchecked")
	@Transactional
	public void updateAceNumWithDirection(Ace entity, MoveDirection direction) {
		Assert.notNull(entity);
		String hql = "";
		String parentType = "";
		EntityObject parentEntity = null;
		int aceNum = entity.getAceNum();
		
		if (entity.getAccessGroup() != null){
			parentType = "accessGroup";
			parentEntity = entity.getAccessGroup();
		} else if (entity.getTemplate() != null){
			parentType = "template";
			parentEntity = entity.getTemplate();
		}else{
			return;
		}
		if (direction == MoveDirection.TOP) {
			// 将自身规则号设置成0
			hql = "UPDATE Ace SET aceNum=0 WHERE aceNum=? AND " + parentType + "=?";
			this.createQuery(hql, aceNum, parentEntity)
					.executeUpdate();
			// 将小于指定规则号的记录的规则都加1
			hql = "UPDATE Ace SET aceNum=aceNum+1 WHERE aceNum<? AND " + parentType + "=?";
			this.createQuery(hql, aceNum, parentEntity)
					.executeUpdate();
		} else if (direction == MoveDirection.UP) {
			// 取得上一条的规则号

			Criteria criteria = createCriteria(Ace.class);
			criteria.add(Restrictions
					.eq(parentType, parentEntity));
			criteria.add(Restrictions.lt("aceNum", aceNum));
			criteria.addOrder(org.hibernate.criterion.Order.desc("aceNum"));
			List<Ace> tempAceList = (List<Ace>) (criteria.list());
			Ace tempAce = null;
			if (tempAceList == null || tempAceList.size() == 0) {
				return;
			} else {
				tempAce = tempAceList.get(0);
			}
			// 将自身规则号设置成0
			hql = "UPDATE Ace SET aceNum=0 WHERE aceNum=? AND " + parentType + "=?";
			this.createQuery(hql, aceNum, parentEntity)
					.executeUpdate();
			// 将上一条设置成该条的规则号
			hql = "UPDATE Ace SET aceNum=? WHERE aceNum=? AND " + parentType + "=?";
			this.createQuery(hql, aceNum, tempAce.getAceNum(),
					parentEntity).executeUpdate();
			// 将该条的规则号设置成上一条的
			hql = "UPDATE Ace SET aceNum=? WHERE aceNum=0 AND " + parentType + "=?";
			this.createQuery(hql, tempAce.getAceNum(), parentEntity)
					.executeUpdate();
			entity.setAceNum(tempAce.getAceNum());
			tempAce.setAceNum(aceNum);
		} else if (direction == MoveDirection.DOWN) {
			// 取得下一条的规则号

			Criteria criteria = createCriteria(Ace.class);
			criteria.add(Restrictions
					.eq(parentType, parentEntity));
			criteria.add(Restrictions.gt("aceNum", aceNum));
			criteria.addOrder(org.hibernate.criterion.Order.asc("aceNum"));
			List<Ace> tempAceList = (List<Ace>) (criteria.list());
			Ace tempAce = null;
			if (tempAceList == null || tempAceList.size() == 0) {
				return;
			} else {
				tempAce = tempAceList.get(0);
			}
			// 将自身规则号设置成0
			hql = "UPDATE Ace SET aceNum=0 WHERE aceNum=? AND " + parentType + "=?";
			this.createQuery(hql, aceNum, parentEntity)
					.executeUpdate();
			// 将下一条设置成该条的规则号
			hql = "UPDATE Ace SET aceNum=? WHERE aceNum=? AND " + parentType + "=?";
			this.createQuery(hql, aceNum, tempAce.getAceNum(),
					parentEntity).executeUpdate();
			// 将该条的规则号设置成下一条的
			hql = "UPDATE Ace SET aceNum=? WHERE aceNum=0 AND " + parentType + "=?";
			this.createQuery(hql, tempAce.getAceNum(), parentEntity)
					.executeUpdate();
			entity.setAceNum(tempAce.getAceNum());
			tempAce.setAceNum(aceNum);
		} else if (direction == MoveDirection.BOTTOM) {
			// 将自身规则号设置成0
			hql = "UPDATE Ace SET aceNum=0 WHERE aceNum=? AND " + parentType + "=?";
			this.createQuery(hql, aceNum, parentEntity)
					.executeUpdate();
			// 将大于指定规则号的记录的规则都减1
			hql = "UPDATE Ace SET aceNum=aceNum-1 WHERE aceNum>? AND " + parentType + "=?";
			int count = this.createQuery(hql, aceNum, parentEntity)
					.executeUpdate();
			// 将自身规则号设置成最大值

			hql = "UPDATE Ace SET aceNum=? WHERE aceNum=0 AND " + parentType + "=?";
			this.createQuery(hql, aceNum + count, parentEntity)
					.executeUpdate();
		}
	}

	/**
	 * 根据模板取得ACL规则
	 * 
	 * @param template
	 *            模板
	 * @return ACL规则
	 */
	@SuppressWarnings("unchecked")
	public List<Ace> getByTemplate(Template template) {
		Assert.notNull(template);
		Criteria criteria = createCriteria(Ace.class);
		criteria.add(Restrictions.eq("template", template));
		criteria.addOrder(org.hibernate.criterion.Order.asc("aceNum"));
		return (List<Ace>) (criteria.list());
	}
	
	/**
	 * 根据ACL组删除ACL规则
	 * @param timeRange ACL组

	 */
	public void deleteByAccessGroup(AccessGroup accessGroup){
		Assert.notNull(accessGroup);
		String hql = "delete from Ace where accessGroup = ?";
		this.createQuery(hql, accessGroup).executeUpdate();
	}
	/**
	 * 判断ACL规则是否存在
	 * @param entity ACL规则
	 * @return ACL规则是否存在
	 */
	public boolean existByAce(Ace entity){
		Assert.notNull(entity);
		Criteria criteria = createCriteria(Ace.class);
		String aceType = "";
		if (entity.getAccessGroup() != null){
			//为ACL组的规则时

			aceType = entity.getAccessGroup().getAclType();
			criteria.add(Restrictions.eq("accessGroup", entity.getAccessGroup()));
		}else if (entity.getTemplate() != null){
			//为ACL模板的规则时
			aceType = entity.getTemplate().getAccessGroupType();
			criteria.add(Restrictions.eq("template", entity.getTemplate()));
		}else{
			return false;
		}
		
		//标准
		if (AceTypeEnum.STANDARD.toString().equals(aceType)){
			//源匹配类型	所有ip、主机、网段

			if(entity.getSourceMatchType()!=null){
				criteria.add(Restrictions.eq("sourceMatchType", entity.getSourceMatchType()));
			}else{
				criteria.add(Restrictions.isNull("sourceMatchType"));
			}
			//源地址		主机、网段时作用
			if (MatchTypeEnum.host.toString().equals(entity.getSourceMatchType()) ||
					MatchTypeEnum.segment.toString().equals(entity.getSourceMatchType())){
				criteria.add(Restrictions.eq("sourceAddr", entity.getSourceAddr()));
			}
			//源通配符	网段时作用

			if (MatchTypeEnum.segment.toString().equals(entity.getSourceMatchType())){
				criteria.add(Restrictions.eq("sourceWildcard", entity.getSourceWildcard()));
			}
		}
		//扩展
		else if (AceTypeEnum.EXTENDED.toString().equals(aceType)){
			//协议类型，不可以为空
			if(entity.getProtocolType()!=null){
				criteria.add(Restrictions.eq("protocolType", entity.getProtocolType()));
			}else{
				criteria.add(Restrictions.isNull("protocolType"));
			}
			//源匹配类型	所有ip、主机、网段

			if(entity.getSourceMatchType()!=null){
				criteria.add(Restrictions.eq("sourceMatchType", entity.getSourceMatchType()));
			}else{
				criteria.add(Restrictions.isNull("sourceMatchType"));
			}
			//源地址		主机、网段时作用
			if (MatchTypeEnum.host.toString().equals(entity.getSourceMatchType()) ||
					MatchTypeEnum.segment.toString().equals(entity.getSourceMatchType())){
				criteria.add(Restrictions.eq("sourceAddr", entity.getSourceAddr()));
			}
			//源通配符	网段时作用

			if (MatchTypeEnum.segment.toString().equals(entity.getSourceMatchType())){
				criteria.add(Restrictions.eq("sourceWildcard", entity.getSourceWildcard()));
			}
			//源端口	协议类型为tcp udp时作用，可以为空
			if (ProtocolTypeEnum.tcp.toString().equals(entity.getProtocolType()) ||
					ProtocolTypeEnum.udp.toString().equals(entity.getProtocolType())){
				if(entity.getSourcePort()!=null){
					criteria.add(
							Restrictions.or(Restrictions.eq("sourcePort", entity.getSourcePort())
									, Restrictions.eq("sourcePort", PortTypeEnum.toggle(entity.getSourcePort(),entity.getProtocolType()))));
				}else{
					criteria.add(Restrictions.isNull("sourcePort"));
				}
			}
			//目的匹配类型		所有ip、主机、网段

			if(entity.getDesMatchType()!=null){
				criteria.add(Restrictions.eq("desMatchType", entity.getDesMatchType()));
			}else{
				criteria.add(Restrictions.isNull("desMatchType"));
			}
			//目的地址		主机、网段时作用
			if (MatchTypeEnum.host.toString().equals(entity.getDesMatchType()) ||
					MatchTypeEnum.segment.toString().equals(entity.getDesMatchType())){
				criteria.add(Restrictions.eq("desAddr", entity.getDesAddr()));
			}
			//目的通配符		网段时作用

			if (MatchTypeEnum.segment.toString().equals(entity.getDesMatchType())){
				criteria.add(Restrictions.eq("desWildcard", entity.getDesWildcard()));
			}
			//目的端口	协议类型为tcp udp时作用，可以为空
			if (ProtocolTypeEnum.tcp.toString().equals(entity.getProtocolType()) ||
					ProtocolTypeEnum.udp.toString().equals(entity.getProtocolType())){
				if(entity.getDesPort()!=null){
					criteria.add(
							Restrictions.or(Restrictions.eq("desPort", entity.getDesPort())
									, Restrictions.eq("desPort", PortTypeEnum.toggle(entity.getDesPort(),entity.getProtocolType()))));
				}else{
					criteria.add(Restrictions.isNull("desPort"));
				}
			}
		}
		//MAC
		else if (AceTypeEnum.MAC.toString().equals(aceType)){
			//源MAC匹配类型	所有mac、主机

			if(entity.getSourceMacMatchType()!=null){
				criteria.add(Restrictions.eq("sourceMacMatchType", entity.getSourceMacMatchType()));
			}else{
				criteria.add(Restrictions.isNull("sourceMacMatchType"));
			}
			//源MAC地址	主机时作用

			if (MacMatchTypeEnum.host.toString().equals(entity.getSourceMacMatchType())){
				criteria.add(Restrictions.eq("sourceMacAddr", entity.getSourceMacAddr()));
			}
			//目的MAC匹配类型	所有mac、主机

			if(entity.getDesMacMatchType()!=null){
				criteria.add(Restrictions.eq("desMacMatchType", entity.getDesMacMatchType()));
			}else{
				criteria.add(Restrictions.isNull("desMacMatchType"));
			}
			//目的MAC地址		主机时作用

			if (MacMatchTypeEnum.host.toString().equals(entity.getDesMacMatchType())){
				criteria.add(Restrictions.eq("desMacAddr", entity.getDesMacAddr()));
			}
			//以太网协议类型，可以为空
			if(entity.getEthernetType()==null
					||entity.getEthernetType().trim().length()==0
					||EthernetTypeEnum.etype_any.toString().equals(entity.getEthernetType())){
				criteria.add(
						Restrictions.or(Restrictions.isNull("ethernetType"),
								Restrictions.or(Restrictions.eq("ethernetType",""),
										Restrictions.eq("ethernetType", EthernetTypeEnum.etype_any.toString()))));
			
			}else{
				criteria.add(Restrictions.eq("ethernetType", entity.getEthernetType()));
			}
		}
		//专家
		else if (AceTypeEnum.EXPERT.toString().equals(aceType)){
			//协议类型，可以为空

			if(entity.getProtocolType()!=null){
				criteria.add(Restrictions.eq("protocolType", entity.getProtocolType()));
			}else{
				criteria.add(Restrictions.isNull("protocolType"));
			}
			//VLan编号，可以为空

			if(entity.getVlanNum()!=null){
				criteria.add(Restrictions.eq("vlanNum", entity.getVlanNum()));
			}else{
				criteria.add(Restrictions.isNull("vlanNum"));
			}
			//源匹配类型	所有ip、主机、网段  当协议类型为以太网时，为空

			if(entity.getSourceMatchType()!=null){
				criteria.add(Restrictions.eq("sourceMatchType", entity.getSourceMatchType()));
			}else{
				criteria.add(Restrictions.isNull("sourceMatchType"));
			}
			//源地址		主机、网段时作用
			if (MatchTypeEnum.host.toString().equals(entity.getSourceMatchType()) ||
					MatchTypeEnum.segment.toString().equals(entity.getSourceMatchType())){
				criteria.add(Restrictions.eq("sourceAddr", entity.getSourceAddr()));
			}
			//源通配符	网段时作用

			if (MatchTypeEnum.segment.toString().equals(entity.getSourceMatchType())){
				criteria.add(Restrictions.eq("sourceWildcard", entity.getSourceWildcard()));
			}
			//源MAC匹配类型	所有mac、主机

			if(entity.getSourceMacMatchType()!=null){
				criteria.add(Restrictions.eq("sourceMacMatchType", entity.getSourceMacMatchType()));
			}else{
				criteria.add(Restrictions.isNull("sourceMacMatchType"));
			}
			//源MAC地址	主机时作用

			if (MacMatchTypeEnum.host.toString().equals(entity.getSourceMacMatchType())){
				criteria.add(Restrictions.eq("sourceMacAddr", entity.getSourceMacAddr()));
			}
			//源端口	协议类型为tcp udp时作用，可以为空
			if (ProtocolTypeEnum.tcp.toString().equals(entity.getProtocolType()) ||
					ProtocolTypeEnum.udp.toString().equals(entity.getProtocolType())){
				if(entity.getSourcePort()!=null){
					criteria.add(
							Restrictions.or(Restrictions.eq("sourcePort", entity.getSourcePort())
									, Restrictions.eq("sourcePort", PortTypeEnum.toggle(entity.getSourcePort(),entity.getProtocolType()))));
				}else{
					criteria.add(Restrictions.isNull("sourcePort"));
				}
			}
			//目的匹配类型		所有ip、主机、网段

			if(entity.getDesMatchType()!=null){
				criteria.add(Restrictions.eq("desMatchType", entity.getDesMatchType()));
			}else{
				criteria.add(Restrictions.isNull("desMatchType"));
			}
			//目的地址		主机、网段时作用
			if (MatchTypeEnum.host.toString().equals(entity.getDesMatchType()) ||
					MatchTypeEnum.segment.toString().equals(entity.getDesMatchType())){
				criteria.add(Restrictions.eq("desAddr", entity.getDesAddr()));
			}
			//目的通配符		网段时作用

			if (MatchTypeEnum.segment.toString().equals(entity.getDesMatchType())){
				criteria.add(Restrictions.eq("desWildcard", entity.getDesWildcard()));
			}
			//目的MAC匹配类型	所有mac、主机

			if(entity.getDesMacMatchType()!=null){
				criteria.add(Restrictions.eq("desMacMatchType", entity.getDesMacMatchType()));
			}else{
				criteria.add(Restrictions.isNull("desMacMatchType"));
			}
			//目的MAC地址		主机时作用

			if (MacMatchTypeEnum.host.toString().equals(entity.getDesMacMatchType())){
				criteria.add(Restrictions.eq("desMacAddr", entity.getDesMacAddr()));
			}
			//目的端口	协议类型为tcp udp时作用，可以为空
			if (ProtocolTypeEnum.tcp.toString().equals(entity.getProtocolType()) ||
					ProtocolTypeEnum.udp.toString().equals(entity.getProtocolType())){
				if(entity.getDesPort()!=null){
					criteria.add(
							Restrictions.or(Restrictions.eq("desPort", entity.getDesPort())
									, Restrictions.eq("desPort", PortTypeEnum.toggle(entity.getDesPort(),entity.getProtocolType()))));
				}else{
					criteria.add(Restrictions.isNull("desPort"));
				}
			}
		}else{
			return false;
		}
		
		//对时间段名的处理
		if (entity.getTimeRangeName() != null&&entity.getTimeRangeName().trim().length()>0){
			criteria.add(Restrictions.eq("timeRangeName", entity.getTimeRangeName()));
		}else{
			criteria.add(Restrictions.isNull("timeRangeName"));
		}
		//对时间段的处理

		if (entity.getTimeRange() != null){
			criteria.add(Restrictions.eq("timeRange", entity.getTimeRange()));
		}else{
			criteria.add(Restrictions.isNull("timeRange"));
		}
		//动作
		criteria.add(Restrictions.eq("actionType", entity.getActionType()));
		
		int count = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
		return count > 0;
	}
	
	
	/**
	 * 设置ACE的时间段信息为NULL或者为空

	 * @param ids 时间段信息

	 */
	public void updateAceByTimeRangeIds(String ids){
		Assert.notNull(ids);
		String hql = "update Ace set timeRange = null ,timeRangeName = null " +
				"where timeRange.id in (" + ids + ") ";
		this.createQuery(hql).executeUpdate();
	}
	
	/**
	 * 把数据库中取得AccessGroup的Entity转换成要下发的AccessGroup信息
	 * @param entity
	 * @return
	 */
	public List<AccessGroupInfo> accessGroup2AccessGroupInfo(List<AccessGroup> accessGroupList){
		List<AccessGroupInfo> accessGroupInfoList = new ArrayList<AccessGroupInfo>();
		if (accessGroupList != null && accessGroupList.size() > 0){
			for(AccessGroup accessGroup : accessGroupList){
				AccessGroupInfo accessGroupInfo = new AccessGroupInfo();
				accessGroupInfo.setAclType(accessGroup.getAclType());
				accessGroupInfo.setName(accessGroup.getName());
				accessGroupInfo.setAceInfoList(AclConfigUtil.ace2AceInfo(getByAccessGroup(accessGroup)));
				accessGroupInfoList.add(accessGroupInfo);
			}
			
		}
		return accessGroupInfoList;
	}
	
}
